package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
	// 订单mapper
	@Autowired
	private OrderMapper orderMapper;
	//地址
	@Autowired
	private AddressMapper addressMapper;
	//订单详情
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	//购物车
	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private CartService cartService;
	
	
	@Override
	public Page<Order> findHistoryOrderPage(Integer page, Integer pageSize) {
		Page<Order> objectPage = new Page<>(page,pageSize);
		//查找当前用户的所有订单
		LambdaQueryWrapper<Order> wrapper1 = new LambdaQueryWrapper<>();
		wrapper1.eq(Order::getUserId,UserHolder.getUser().getId())
				.orderByDesc(Order::getOrderTime);
		objectPage = orderMapper.selectPage(objectPage,wrapper1);
		
		//遍历订单,将订单详情加入到订单中
		//快捷for循环遍历的临时变量是值传递还是址传递
		for (Order record : objectPage.getRecords()) {
			LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(OrderDetail::getOrderId,record.getId());
			List<OrderDetail> orderDetailList = orderDetailMapper.selectList(wrapper);
			
			record.setOrderDetails(orderDetailList);
		}
		return objectPage;
	}
	
	@Override
	public Page<Order> findOrderPage(Integer page, Integer pageSize) {
		Page<Order> orderPage = new Page<>(page,pageSize);
		// LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
		
		orderPage = orderMapper.selectPage(orderPage, null);
		return orderPage;
	}
	
	@Override
	public void submit(Order order) {
		// orderMapper
		// 订单生成(提交)流程,1:生成订单,2:生成订单详情,3:清空购物车
		
		//获取到当前登录用户信息
		User user = UserHolder.getUser();
		
		//查询用户购物车
		List<Cart> cartList = cartService.findList();
		if (CollectionUtil.isEmpty(cartList)) {
			throw new CustomException("购物车是空的,无法生成订单");
		}
		
		//根据前端传入的地址地址id查询地址信息
		Address address = addressMapper.selectById(order.getAddressId());
		
		if (address == null) {
			throw new CustomException("地址不存在,无法生成订单");
		}
		
		//使用雪花算法生成一个id
		//mybatiplus工具生成的雪花算法id
		long orderId = IdWorker.getId();
		//订单总额
		BigDecimal total = new BigDecimal("0");
		//根据购物车中信息生成订单详情
		for (Cart cart : cartList) {
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setName(cart.getName());
			orderDetail.setOrderId(orderId);//当前明细所属订单的id
			orderDetail.setDishId(cart.getDishId());
			orderDetail.setSetmealId(cart.getSetmealId());
			orderDetail.setDishFlavor(cart.getDishFlavor());
			orderDetail.setNumber(cart.getNumber());
			orderDetail.setAmount(cart.getAmount());
			orderDetail.setImage(cart.getImage());
			
			//保存到订单详情表中
			orderDetailMapper.insert(orderDetail);
			
			//每次遍历总金额累加
			//total = total +  amount * number
			total = total.add(cart.getAmount().multiply(new BigDecimal(cart.getNumber())));
		}
		
		//生成订单
		//6. 生成订单
		order.setId(orderId);//使用刚刚生成的orderid
		order.setNumber(orderId + "");//订单号 使用订单id
		order.setStatus(0);//待付款
		order.setUserId(user.getId());//登录用户id
		order.setOrderTime(new Date());//下单时间
		order.setCheckoutTime(new Date());//支付时间(支付的时候再更新一下)
		order.setAmount(total);//本次订单总金额=所有明细中单价*数量再相加
		order.setUserName(user.getName());
		order.setPhone(address.getPhone());
		order.setAddress(address.getDetail());
		order.setConsignee(address.getConsignee());
		orderMapper.insert(order);
		
		//清空购物车
		cartService.clean();
	}
}
